package is.server.controller.session.local.command;

import is.common.interfaces.Command;
import is.server.Conf;
import is.server.Locale;
import is.server.Main;

import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

/**
 * @author aspr0
 *         Date: 06.04.12
 *         Time: 23:07
 */
public class ConcatCommand implements Command
{
    private String[] params;

    public ConcatCommand(String[] params)
    {
        this.params = params;
    }

    public void execute()
    {
        if (params.length < 2)
        {
            Main.getController().getUserInterface().exposeErrMsg("incorrect parameters"); //todo
            return;
        }

        String server = params[0];

        int portAlien = Conf.PORT;

        try
        {
            portAlien = Integer.parseInt(params[1]);
        }
        catch (NumberFormatException e)
        {
            Main.getController().getUserInterface().exposeErrMsg("Port should be number"); //todo
        }

        if (Main.getController().getModel().isActivated())
        {
            try
            {
                int rdc = Main.getController().getModel().concat(server, portAlien);
                Main.getController().getUserInterface().exposeErrMsg("Removed %s duplicates", rdc); //todo
            }
            catch (ConnectException e)
            {
                Main.getController().getUserInterface().exposeErrMsg(Locale.ERR_CONNECT);
//            e.printStackTrace();
            }
            catch (UnknownHostException e)
            {
                Main.getController().getUserInterface().exposeErrMsg(Locale.ERR_UNKNOWN_HOST);
//            e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
        }
        else
        {
            Main.getController().getUserInterface().exposeErrMsg("model deactivated"); //todo
        }
    }
}
